package com.recruit.g1.job.controller;

import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import com.recruit.g1.common.response.JsonResult;
import com.recruit.g1.job.mapper.JobMapper;
import com.recruit.g1.job.service.InterestedJobsService;
import com.recruit.g1.pojo.job.dto.JobInsertParam;
import com.recruit.g1.pojo.job.dto.JobListQuery;
import com.recruit.g1.pojo.job.entity.Job;
import com.recruit.g1.pojo.job.vo.JobListVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import javax.servlet.http.HttpSession;
import java.util.Date;
import java.util.List;

@Slf4j
@Api(tags = "岗位管理模块")
@RestController
@RequestMapping("/job")
public class JobController {

    @Autowired
    private JobMapper jobMapper;

    @PostMapping("insert")
    @ApiOperation("发布岗位")
    @ApiOperationSupport(order = 10)
    public JsonResult insert(@RequestBody JobInsertParam jobInsertParam, @ApiIgnore HttpSession session) {
        log.debug("jobInsertParam = " + jobInsertParam);
        //登陆状态
        //UserSaveParam userSaveParam = (UserSaveParam) session.getAttribute(session.getId());
        /*User userSaveParam =(User) session.getAttribute("currentLoginUser");

        if (userSaveParam == null){
            return JsonResult.fail(StateCode.NOT_LOGIN);
        }*/
        Job job = new Job();
        BeanUtils.copyProperties(jobInsertParam, job);
        job.setPostTime(new Date());
        jobMapper.insertJob(job);
        return JsonResult.ok("发布成功");
    }



    @PostMapping("list")
    @ApiOperation("查询岗位列表")
    @ApiOperationSupport(order = 20)
    public JsonResult select(JobListQuery jobListQuery) {
        log.debug("jobListQuery = " + jobListQuery);

        String jobname = jobListQuery.getJobName();
        Long cityid = jobListQuery.getCityId();

        List<JobListVO> jobListVOList = jobMapper.jobList(jobname, cityid);
        return JsonResult.ok(jobListVOList);
    }

    @PostMapping("delete/{id}")
    @ApiOperation("删除岗位")
    @ApiOperationSupport(order = 30)
    public JsonResult delete(@PathVariable Integer id, @ApiIgnore HttpSession session) {
        log.debug("id = " + id);

        //UserSaveParam userSaveParam = (UserSaveParam) session.getAttribute(session.getId());
        /*User userSaveParam =(User) session.getAttribute("currentLoginUser");

        if (userSaveParam == null){
            return JsonResult.fail(StateCode.NOT_LOGIN);
        }*/

        // 检查id是否为null
        if (id == null) {
            return JsonResult.error("ID cannot be null");
        }

        jobMapper.deleteJob(id);


        return JsonResult.ok("删除成功");
    }






















/// //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////





    @Autowired
    InterestedJobsService interestedJobsService;
    @GetMapping("/select/{userId}")
    @ApiOperation("查询感兴趣岗位")
    @ApiOperationSupport(order = 10)
    public JsonResult selectInterestedJobs(@PathVariable Long userId){
        log.debug("传过来的userID的值为{}",userId);
        List<Job> list = interestedJobsService.selectInterestedJobs(userId);
        return JsonResult.ok(list);

    }
    @GetMapping("/selectJob/{userId}/{jobId}")
    @ApiOperation("查询具体感兴趣岗位")
    @ApiOperationSupport(order = 20)
    public JsonResult selectInterestedJob(@PathVariable Long userId,@PathVariable Long jobId){
        log.debug("传过来的userID的值为"+userId+"传过来的jobId的值为"+jobId);
        List<Job> list = interestedJobsService.selectInterestedJob(userId, jobId);
        return JsonResult.ok(list);

    }
    @PostMapping("/delete/{jobId}")
    @ApiOperation("取消关注感兴趣岗位")
    @ApiOperationSupport(order = 30)//本方法因为没有处理user模块登录成功，本模块获取不到登录用户的问题，所以本方法目前还无法使用
    public JsonResult deleteInterestedJobs(@PathVariable Long jobId, HttpSession session){
        interestedJobsService.deleteInterestedJobs(jobId,session);
        return JsonResult.ok();
    }










}
